import { Modal, Message, Notification } from '@arco-design/web-vue'

/**
 * 删除
 * @param record 一般为列表单条数据
 * @param callback
 */
export const useDelete = function (api: any, queryParam: any) {
  return new Promise<void>((resolve) => {
    Modal.confirm({
      title: '确认删除',
      simple: true,
      content: '是否要删除此数据，删除后不恢复？',
      okText: '删除',
      onBeforeOk: async () => {
        await api(queryParam)
        Message.success('删除成功')
        resolve()
        return true
      },
    })
  })
}

/**
 * 询问
 * @param record 一般为列表单条数据
 * @param callback
 */
export const useConfirm = function (api: any, queryParam: any, confirmConfig: object) {
  Modal.confirm({
    title: '确认',
    simple: true,
    content: '是否要删除此数据，删除后不恢复？',
    okText: '删除',
    ...confirmConfig,
    onBeforeOk: async () => {
      await api(queryParam)
      Message.success('删除成功')
      return true
    },
  })
}

/**
 * 导出
 * @param api 导出的 api
 * @param queryParam 导出 api 对应的参数
 */
export const useExport = function (api: any, queryParam: any) {
  return new Promise(async (resolve, reject) => {
    try {
      await api(queryParam)
      Notification.success({
        title: '导出成功',
        closable: true,
        content: '请点击页面右上方的【导出】按钮查看结果',
      })
      resolve()
    } catch (error) {
      reject()
    }
  })
}

/**
 * 组件库默认 sortDirections 改为接口对应值
 * 更新传入 queryParam 对应字段
 */
export const useSortChange = function (prop: any, direction: any, queryParam: any) {
  const dir = direction ? (direction === 'ascend' ? 'asc' : 'desc') : undefined
  if (dir) {
    queryParam['sortField'] = prop
    queryParam['sortType'] = dir
  } else {
    queryParam['sortField'] = undefined
    queryParam['sortType'] = undefined
  }
}
